<template>
	<view>
		<view class="page">
			<view class="con">
				<view class="tis">药方名称</view>
				<u-input
					placeholder="请输入药方名称"
					clearable
					v-model="yaofang.name"
					style="border-bottom: 1rpx solid #F5F5F5;"
				></u-input>
			</view>
			<view class="con">
				<view class="tis">
					<view>
						<text>关联病症</text>
					</view>
					<view>
						<u-icon label="添加" color="#FF8585" size="20" name="plus-circle-fill" @click="add_goto_zdpage"></u-icon>
					</view>
				</view>
				<view class="bingzheng_list">
					<view class="li_item" v-for="(zd,zd_index) in yaofang.guanlian_zd">
						<u-icon :label="zd.name" labelPos="left" color="#FF8585" size="20" name="minus-circle-fill"></u-icon>
					</view>
				</view>
			</view>
			<view class="con">
				<view class="tis">药方</view>
				<view class="bingzheng_list">
					<u-input
						class="yaofangbox" 
						type="textarea"
						placeholder="请输入药方名称"
						clearable
						v-model="yaofang.text1"
					></u-input>
				</view>
			</view>
			<view class="con">
				<view class="tis">价格(元)</view>
				<u-input
					placeholder="请输入价格"
					clearable
					v-model="yaofang.price"
					style="border-bottom: 1rpx solid #F5F5F5;"
					
				></u-input>
			</view>
			<view class="btn_list">
				<u-button @click="yaofang_save">保存</u-button>
				<u-button>返回</u-button>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>
<script>
	import { get_yaofang_info,save_yaofang } from '@/util/api/api.js';
	
	export default {
		data() {
			return {
				yaofang:{
					id:0,
					name:'',
					guanlian_zd:[],
					text1:'',
					price:800.00
				}
			}
		},
		onLoad(e) {
			this.init_yaofang_page(e.id)
			
			let bz_list = getApp().globalData.yf_guanlian_zd
			
			if(bz_list !== undefined && bz_list.length>0){
				this.yaofang.guanlian_zd = bz_list
				getApp().globalData.yf_guanlian_zd = []
			}
		},
		methods: {
			async init_yaofang_page(id){
				let that = this
				if (id>0) {
					await get_yaofang_info({id}).then((res)=>{
						if (res.code==200) {
							console.log(res)
							that.yaofang = res.data
						} else{
							that.showToast(res.message)
						}
					})
				} else{
					this.yaofang = {
						id:0,
						name:'',
						guanlian_zd:[],
						text1:'',
						text2:'',
						price:'800.00'
					}
				}
			},
			add_goto_zdpage(){
				getApp().globalData.yf_guanlian_zd =  this.yaofang.guanlian_zd

				uni.navigateTo({
					url: '../fenlei/fenlei?page_type=2'
				});
			},
			showToast(text) {
				this.$refs.uToast.show({
					title: text,
					// url: '/pages/user/index'
				})
			},
			async yaofang_save(id = 0){
				let that = this
				let add_data = {}
				
				add_data.name = that.yaofang.name
				add_data.text1 = that.yaofang.text1
				// add_data.text2 = that.yaofang.text2
				add_data.price = that.yaofang.price
				
				if(that.yaofang.guanlian_zd.length==0){
					that.showToast('请添加关联的病症')
					return
				}else{
					let guanlian_zd_ids = []
					that.yaofang.guanlian_zd
					for (let i=0;i<that.yaofang.guanlian_zd.length;i++) {
						guanlian_zd_ids.push(that.yaofang.guanlian_zd[i].id)
					}
					
					add_data.guanlian_zd_ids = guanlian_zd_ids.join(',')
				}
				
				if(that.yaofang.id>0){
					add_data.id = that.yaofang.id
				}
				
				let res = await save_yaofang(add_data)
				
				if (res.code==200) {
					that.showToast(res.msg)
					
					setTimeout(()=>{
						this.go_back()
					},800)
					// that.page_init()
					// that.hide_add_class_popup()
				} else{
					that.showToast(res.msg)
				}
			},
			go_back(){
				uni.navigateBack({
					delta:1
				})
			}
		}
	}
</script>
<style scoped>
.page{
	padding: 50rpx;
}
.tis{
	display:flex;
	justify-content: space-between;
	font-size: 30rpx;
	font-weight: bold;
	color: #666666;
	line-height: 77rpx;
}
.btn_list{
	width: 650rpx;
	margin-top: 140rpx;
	display: flex;
}
.btn_list .u-btn{
	width: 300rpx;
	height: 88rpx;
	background: #15BBA2;
	border-radius: 44px;
	color: #FFFFFF;
}
.bingzheng_list{
	min-height: 100rpx;
	background: #F5F5F5;
	border-radius: 8rpx;
	padding: 15rpx;
	overflow-y: scroll;
}
.yaofangbox{
	height: 225rpx;
	background: #F5F5F5;
	border-radius: 8rpx;
	border: 0;
	padding: 15rpx;
}
.li_item{
	display: inline-block;
	background: #FFFFFF;
	padding: 10rpx 20rpx;
	margin: 10rpx;
}
</style>